from utils.log_util import logger
from utils.mysql_util import db

"""
与数据库相连接，在数据库进行操作

注意：
查询：db.select_db_one  有返回值
执行：db.execute_db  无返回值
"""

# 获取短信验证码
def get_code(mobile):
    sql = "select code from users_verifycode where mobile = '%s' order by id desc limit 1" % mobile
    result = db.select_db_one(sql)
    logger.info(f'sql执行结果:{result}')
    return result['code']

# 删除用户
def delete_user(mobile):
    sql = "delete from users_userprofile where mobile = '%s';" % mobile
    result = db.execute_db(sql)
    logger.info(f'sql删除成功:{result}')

# 删除验证码
def delete_code(mobile):
    sql = "delete from users_verifycode where mobile = '%s';" % mobile
    result = db.execute_db(sql)
    logger.info(f'sql删除验证码成功:{result}')

# #查询user_id
# def user_id(mobile):
#     sql = "select id from users_userprofile where mobile = '%s';" % mobile
#     result = db.select_db_one(sql)
#     logger.info(f'sql查询user_id:{result}')
#     return result['id']

# #查询购物车商品数
# def get_shopping_cart_num(mobile, goods_id):
#     userID = user_id(mobile)
#     sql = "select * from trade_shoppingcart where user_id = %s and goods_id = %d;" % (userID,goods_id)
#     result = db.select_db_one(sql)
#     # logger.info(f'sql查询购物车商品数:{result}')
#     return result['nums']

 # 查询购物车某商品个数
def get_shopping_cart_num(username, good_id):
    id = user_id(username)
    sql = "select nums from trade_shoppingcart where user_id = %d and goods_id = %d" % (id, good_id)
    result = db.select_db_one(sql)
    return result['nums']


# 查询userid
def user_id(mobile):
    sql = "select id from users_userprofile where mobile = '%s';" % mobile
    result = db.select_db_one(sql)
    return result['id']